﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TLAGPL
    {

        public TLAGPL()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VLAGPL();
            this.DataContext = MV;
            InitializeComponent();
        }
        bool IsNewLO = false;
        bool IsNewLB = false;
        bool IsNewLP = false;


        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["LPMANDANT"] = app.Mandant;
            dwData["LPLO"] = "AND LPLO LIKE '" + LO.Trim() + "%'";
            dwData["LPLB"] = "AND LPLB LIKE'" + LB.Trim() + "%'";
            dwData["LPPLATZ"] = "AND LPPLATZ LIKE '%'";
            SResult = ((VLAGPL)MV).QueryLAGPL(dwData, 5);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                IsNewLO = true;
                IsNewLB = false;
                IsNewLP = false;
                return;
            }
            if (LO.Trim().Length == 0)
            {
                MessageBox.Show("库不能为空");
                return;
            }
            if (IsNewLO)
            {
                dwData = TBMain.NewRow();
                ((VLAGPL)MV).SetUpNewRow(ref dwData);
                dwData["LPMANDANT"] = app.Mandant;
                dwData["LPLO"] = LO;
                ((VLAGPL)MV).Insert(dwData);
                vwMaster.View.MoveCurrentToFirst();
            }
            if (IsNewLB)
            {
                NewLBCommand(sender, e);
            }
            if (IsNewLP)
            {
                NewLPCommand(sender, e);
            }
            IsNewLO = false;
            IsNewLB = false;
            IsNewLP = false;
            IsNew = false;
            base.NewCommand(sender, e);
        }

        public void NewLBCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                IsNewLO = false;
                IsNewLB = true;
                IsNewLP = false;
                return;
            }
            if (LO.Trim().Length == 0)
            {
                MessageBox.Show("库不能为空");
                return;
            }
            if (LB.Trim().Length == 0)
            {
                MessageBox.Show("库区不能为空");
                return;
            }
            dwData = TBMain.NewRow();
            ((VLAGPL)MV).SetUpNewRow(ref dwData);
            dwData["LPMANDANT"] = app.Mandant;
            dwData["LPLO"] = LO;
            dwData["LPLB"] = LB;
            ((VLAGPL)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNewLO = false;
            IsNewLB = false;
            IsNewLP = false;
            IsNew = false;
            base.NewCommand(sender, e);
        }

        public void NewLPCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                IsNewLO = false;
                IsNewLB = false;
                IsNewLP = true;
                return;
            }
            if (LO.Trim().Length == 0)
            {
                MessageBox.Show("库不能为空");
                return;
            }
            if (LB.Trim().Length == 0)
            {
                MessageBox.Show("库区不能为空");
                return;
            }
            if (LP.Trim().Length == 0)
            {
                MessageBox.Show("库位不能为空");
                return;
            }
            dwData = TBMain.NewRow();
            ((VLAGPL)MV).SetUpNewRow(ref dwData);
            dwData["LPMANDANT"] = app.Mandant;
            dwData["LPLO"] = LO;
            dwData["LPLB"] = LB;
            dwData["LPPLATZ"] = LP;
            ((VLAGPL)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNewLO = false;
            IsNewLB = false;
            IsNewLP = false;
            IsNew = false;
            base.NewCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VLAGPL)MV).DeleteLAGPL(dwMaster, 1);
            filter = "LPLO='" + dwMaster["LPLO"].ToString() + "' AND LPLB='" + dwMaster["LPLB"].ToString() + "' AND LPPLATZ='" + dwMaster["LPPLATZ"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return; 
            dwData = TBMain.NewRow();
            dwData["LPMANDANT"] = app.Mandant;
            dwData["LPLO"] = "AND LPLO='" + ORDERNUM.Substring(0, 2) + "'";
            dwData["LPLB"] = "AND LPLB='" + ORDERNUM.Substring(2, 2) + "'";
            dwData["LPPLATZ"] = "AND LPPLATZ='" + ORDERNUM.Substring(4, 8) + "'";
            SResult = ((VLAGPL)MV).QueryLAGPL(dwData, 5);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            } 
            base.LOADBYAUF();
        }
    }
}
